package com.blrs.service.enterprise.records.impl;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.blrs.common.utils.SecurityUtils;
import com.blrs.common.utils.StringUtils;
import com.blrs.domain.ElectricSalesFollowRecords;
import com.blrs.domain.EnterpriseFollowRecords;
import com.blrs.domain.EnterpriseSatisfied;
import com.blrs.domain.HighSeaRecord;
import com.blrs.mapper.*;
import com.blrs.service.enterprise.records.IEnterpriseFollowRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 企业跟进记录Service业务层处理
 * 
 * @author yuxiang.chen
 * @date 2023-08-08
 */
@Service
public class EnterpriseFollowRecordsServiceImpl implements IEnterpriseFollowRecordsService
{
    @Autowired
    private EnterpriseFollowRecordsMapper enterpriseFollowRecordsMapper;
    @Autowired
    private EnterpriseInfoMapper enterpriseInfoMapper;
    @Autowired
    private EnterpriseSatisfiedMapper enterpriseSatisfiedMapper;
    @Autowired
    private HighSeaRecordMapper highSeaRecordMapper;
    @Autowired
    private ElectricSalesFollowRecordsMapper electricSalesFollowRecordsMapper;


    /**
     * 查询企业跟进记录
     * 
     * @param recordsId 企业跟进记录主键
     * @return 企业跟进记录
     */
    @Override
    public EnterpriseFollowRecords selectEnterpriseFollowRecordsByRecordsId(String recordsId)
    {
        EnterpriseFollowRecords enterpriseFollowRecords = enterpriseFollowRecordsMapper.selectEnterpriseFollowRecordsByRecordsId(recordsId);
        if (enterpriseFollowRecords.getType().equals("5")){
            //说明是满意度调查
            EnterpriseSatisfied enterpriseSatisfied = enterpriseSatisfiedMapper.selectEnterpriseSatisfiedByRecordId(recordsId);
            enterpriseFollowRecords.setEnterpriseSatisfied(enterpriseSatisfied);
        }
        return enterpriseFollowRecords;
    }

    /**
     * 查询企业跟进记录列表
     *
     * @param enterpriseFollowRecords 企业跟进记录
     * @return 企业跟进记录
     */
    @Override
    public List<EnterpriseFollowRecords> selectEnterpriseFollowRecordsList(EnterpriseFollowRecords enterpriseFollowRecords) {
        List<EnterpriseFollowRecords> enterpriseFollowRecords1 = enterpriseFollowRecordsMapper.selectEnterpriseFollowRecordsList(enterpriseFollowRecords);
      return enterpriseFollowRecords1;
    }


    /**
     * 新增企业跟进记录
     * 
     * @param enterpriseFollowRecords 企业跟进记录
     * @return 结果
     */
    @Override
    public int insertEnterpriseFollowRecords(EnterpriseFollowRecords enterpriseFollowRecords)
    {
       /* Long count =enterpriseInfoMapper.count(enterpriseFollowRecords.getEnterpriseId());
        if(count==0){
           throw  new ServiceException("请先保存企业信息！");
        }*/
       /* Long size=enterpriseFollowRecordsMapper.size(enterpriseFollowRecords.getRecordsId());
        if(size==0){
            enterpriseFollowRecords.setDelFlag("0");
            enterpriseFollowRecords.setFollowId(SecurityUtils.getUserId());
            enterpriseFollowRecords.setFollowPerson(SecurityUtils.getUsername());
        }*/
        enterpriseFollowRecords.setDelFlag("0");
        enterpriseFollowRecords.setFollowId(SecurityUtils.getUserId());
        enterpriseFollowRecords.setFollowPerson(SecurityUtils.getUsername());
        enterpriseFollowRecords.setFollowTime(new Timestamp(new Date().getTime()));
        if (enterpriseFollowRecords.getType().equals("5")){
            //说明是满意度调查
            EnterpriseSatisfied enterpriseSatisfied = enterpriseFollowRecords.getEnterpriseSatisfied();
            enterpriseSatisfied.setCreateBy(SecurityUtils.getUserId().toString());
            enterpriseSatisfied.setCreateTime(new Timestamp(new Date().getTime()));
            enterpriseSatisfied.setUpdateBy(SecurityUtils.getUserId().toString());
            enterpriseSatisfied.setUpdateTime(new Timestamp(new Date().getTime()));
            enterpriseSatisfied.setRecordId(enterpriseFollowRecords.getRecordsId());
            enterpriseSatisfied.setEnterpriseId(enterpriseFollowRecords.getEnterpriseId());
            enterpriseSatisfied.setId(UUID.randomUUID().toString().replaceAll("-", ""));
            enterpriseSatisfiedMapper.insertEnterpriseSatisfied(enterpriseSatisfied);
        }

        return  enterpriseFollowRecordsMapper.insertEnterpriseFollowRecords(enterpriseFollowRecords);
    }

    /**
     * 修改企业跟进记录
     * 
     * @param enterpriseFollowRecords 企业跟进记录
     * @return 结果
     */
    @Override
    public int updateEnterpriseFollowRecords(EnterpriseFollowRecords enterpriseFollowRecords)
    {
        /*String id=enterpriseFollowRecords.getRecordsId();
        String ann=enterpriseFollowRecords.getAnnotations();
        String type=enterpriseFollowRecords.getType();
        enterpriseFollowRecords=new EnterpriseFollowRecords();
        enterpriseFollowRecords.setRecordsId(id);
        enterpriseFollowRecords.setType(type);
        enterpriseFollowRecords.setAnnotations(ann);*/
        if(StringUtils.isNotEmpty(enterpriseFollowRecords.getAnnotations())){
            enterpriseFollowRecords.setAnnotationsId(SecurityUtils.getUserId());
            enterpriseFollowRecords.setAnnotationsName(SecurityUtils.getUsername());
        }else {
           if (enterpriseFollowRecords.getType().equals("5")){
               EnterpriseSatisfied enterpriseSatisfied = enterpriseFollowRecords.getEnterpriseSatisfied();
               enterpriseSatisfied.setUpdateBy(SecurityUtils.getUserId().toString());
               enterpriseSatisfied.setUpdateTime(new Timestamp(new Date().getTime()));
               enterpriseSatisfiedMapper.updateEnterpriseSatisfied(enterpriseSatisfied);
           }
        }
        enterpriseFollowRecords.setFollowTime(new Timestamp(new Date().getTime()));
        return enterpriseFollowRecordsMapper.updateEnterpriseFollowRecords(enterpriseFollowRecords);
    }

    /**
     * 批量删除企业跟进记录
     * 
     * @param recordsIds 需要删除的企业跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseFollowRecordsByRecordsIds(String[] recordsIds)
    {
        return deleteEnterpriseFollowRecordsByRecordsId(recordsIds[0]);
    }

    /**
     * 删除企业跟进记录信息
     * 
     * @param recordsId 企业跟进记录主键
     * @return 结果
     */
    @Override
    public int deleteEnterpriseFollowRecordsByRecordsId(String recordsId)
    {
        EnterpriseFollowRecords enterpriseFollowRecords=new EnterpriseFollowRecords();
        enterpriseFollowRecords.setRecordsId(recordsId);
        enterpriseFollowRecords.setDelFlag("1");
        return enterpriseFollowRecordsMapper.updateEnterpriseFollowRecords(enterpriseFollowRecords);
    }

    @Override
    public List<Object> selectEnterpriseFollowRecordsListPerson(EnterpriseFollowRecords enterpriseFollowRecords) {
        List<EnterpriseFollowRecords> enterpriseFollowRecordsList = enterpriseFollowRecordsMapper.selectEnterpriseFollowRecordsList(enterpriseFollowRecords);

        if (!enterpriseFollowRecordsList.isEmpty()) {
            return new ArrayList<Object>(enterpriseFollowRecordsList);
        }

        HighSeaRecord highSeaRecord = new HighSeaRecord();
        highSeaRecord.setEnterprisePersonId(enterpriseFollowRecords.getEnterprisePersonId());
        List<HighSeaRecord> highSeaRecordsList = highSeaRecordMapper.selectHighSeaRecordList(highSeaRecord);

        if (!highSeaRecordsList.isEmpty()) {
            return new ArrayList<Object>(highSeaRecordsList);
        }

        ElectricSalesFollowRecords electricSalesFollowRecords = new ElectricSalesFollowRecords();
        electricSalesFollowRecords.setEnterprisePersonId(enterpriseFollowRecords.getEnterprisePersonId());
        List<ElectricSalesFollowRecords> electricSalesFollowRecordsList = electricSalesFollowRecordsMapper.selectElectricSalesFollowRecordsList(electricSalesFollowRecords);

        return new ArrayList<Object>(electricSalesFollowRecordsList);
    }


}
